<?php

    /**
     * Abstract
     *
     * Abstract class from which each module controller should inherit
     *
     * @package      Core
     * @subpackage   Controller_Action
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
	abstract class Core_Controller_Action_Abstract extends Core_Object
	{

	////////////////////////////////////////////////////////////////////////////
	//                                Variables                               //
	////////////////////////////////////////////////////////////////////////////

	    /**
	     * Request object
	     *
	     * @var Core_Controller_Request_Abstract
	     */
	    protected $_request = null;

		/**
		 * Zend_Registry object
		 *
		 * @var Zend_Registry
		 */
		protected $_registry = null;

	    /**
	     * Parameters for plugins
	     *
	     * This array should contains all parameters needed by installed plugins for
	     * each action, see the documentation of each plugins to know this parameters.
	     * keys : name of an action or * for parameters to all action
	     * values : array that contains all plugin parameters
	     *
	     * @var array
	     */
	    protected $_pluginParams = array();

	////////////////////////////////////////////////////////////////////////////
	//                            Getters / Setters                           //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Returns the plugin parameters, or a parameter from it's index
         *
         * @param       string      $index : an offset in the array
         * @return      array|mixed
         */
        public function getPluginParams($index = null)
        {
            if ($index === null) {
				return $this->_pluginParams;
			} else {
				return $this->_pluginParams[$index];
			}
        }

        /**
         * Adds or changes the value of a plugin parameter
         *
         * @param       string      $index : parameter name
         * @param       string      $value : parameter value
         * @throws		Core_ArgumentNullException
         * @return      void
         */
        public function setPluginParam($index, $value)
        {
            if ($index === null) {
				throw new Core_ArgumentNullException('index is a null reference');
			}

            $this->_pluginParams[$index] = $value;
        }

	    /**
	     * Sets the value of a request parameter.
	     *
	     * @param        string      $key :     key of the parameter to set
	     * @param        string      $value :   value of the parameter to set
	     * @throws		 Core_ArgumentNullException
	     * @return       mixed|null   the request parameter value
	     */
	    protected function _setParam($key, $param)
		{
            if ($param === null) {
				throw new Core_ArgumentNullException('param is a null reference');
			}

			if ($this->_request instanceof Core_Controller_Request_Abstract) {
				return $this->_request->setParam($key, $param);
			} else {
				return null;
			}
	    }

	    /**
	     * Gets the value of a request parameter. If not defined, gets its default value.
	     *
	     * @param        string      $param : the name of the request parameter
	     * @param        mixed       $defaultValue : the default returned value if the parameter doesn't exists
	     * @param        boolean     $useDefaultIfEmpty : true if the default value should be returned if value is empty
	     * @throws		 Core_ArgumentNullException
	     * @return       mixed|null   the request parameter value
	     */
	    protected function _getParam($param, $defaultValue = null, $useDefaultIfEmpty = false)
		{
            if ($param === null) {
				throw new Core_ArgumentNullException('param is a null reference');
			}

			if ($this->_request instanceof Core_Controller_Request_Abstract) {
				return $this->_request->getParam($param, $defaultValue, $useDefaultIfEmpty);
			} else {
				return null;
			}
	    }

	    /**
	     * Gets all parameters
	     *
	     * @return      array|null   all request parameters
	     */
	    protected function _getParams()
		{
			if ($this->_request instanceof Core_Controller_Request_Abstract) {
				return $this->_request->getParams();
			} else {
				return null;
			}
		}

	    /**
	     * Gets a response object
	     *
	     * @param       string      $type : the type of the response type (ex: "http")
		 * @throws		Core_ArgumentException
		 * @throws		Core_ArgumentNullException
	     * @return      Core_Controller_Response_Abstract|null   the response object
	     */
	    protected function _getResponse($type)
		{
            if ($type === null) {
				throw new Core_ArgumentNullException('type is a null reference');
			}

            if (strlen(trim($type)) == 0) {
				throw new Core_ArgumentException('type is a zero-length string');
			}

			if ($this->_request instanceof Core_Controller_Request_Abstract) {
				return $this->_request->getResponse($type);
			} else {
				return null;
			}
	    }

	////////////////////////////////////////////////////////////////////////////
	//                                 Methods                                //
	////////////////////////////////////////////////////////////////////////////

	    /**
	     * Constructor
	     *
	     * @param       Core_Controller_Request_Abstract    $request : the current request object
	     */
	    public function __construct(Core_Controller_Request_Abstract $request)
		{
	        $this->_request     = $request;
	        $this->_registry    = Zend_Registry::getInstance();
	    }

	    /**
         * Destructor
         */
        public function __destruct()
        {
            unset($this);
        }

	////////////////////////////////////////////////////////////////////////////
	//                            Protected Methods                           //
	////////////////////////////////////////////////////////////////////////////

	    /**
	     * Same as _getParam(), but casts the value to an integer value.
	     * If it isn't a numerical value, return null.
	     *
	     * @param        string      $param : the name of the request parameter
	     * @param        mixed       $defaultValue : the default returned value if the parameter doesn't exists
	     * @param        boolean     $useDefaultIfEmpty : true if the default value should be returned if value is empty
	     * @return       integer|null the request parameter value
	     */
	    protected function _intParam($param, $defaultValue = null, $useDefaultIfEmpty = false)
		{
	        $value = $this->_getParam($param, $defaultValue, $useDefaultIfEmpty);

	        if (is_numeric($value)) {
	            return intval($value);
	        } else {
	            return null;
	        }
	    }

	    /**
	     * Same as _getParam(), but casts the value to a float value.
	     * If it isn't a float value, return null.
	     *
	     * @param        string      $param : the name of the request parameter
	     * @param        mixed       $defaultValue : the default returned value if the parameter doesn't exists
	     * @param        boolean     $useDefaultIfEmpty : true if the default value should be returned if value is empty
	     * @return       float|null  the request parameter value
	     */
	    protected function _floatParam($param, $defaultValue = null, $useDefaultIfEmpty = false)
		{
	        $value = $this->_getParam($param, $defaultValue, $useDefaultIfEmpty);

	        if (is_numeric($value)) {
	            return floatval($value);
	        } else {
	            return null;
	        }
	    }

	    /**
	     * Same as _getParam(), but casts the value to a boolean value.
	     * If it isn't a boolean value, return null.
	     *
	     * @param        string      $param : the name of the request parameter
	     * @param        mixed       $defaultValue : the default returned value if the parameter doesn't exists
	     * @param        boolean     $useDefaultIfEmpty : true if the default value should be returned if value is empty
	     * @return       boolean|null the request parameter value
	     */
	    protected function _boolParam($param, $defaultValue = null, $useDefaultIfEmpty = false)
		{
	        $value = $this->_getParam($param, $defaultValue, $useDefaultIfEmpty);

	        if ($value == "true" || $value == "1" || $value == "on" || $value == "yes") {
				return true;
			} else if ($value == "false" || $value == "0" || $value == "off" || $value == "no") {
	            return false;
			} else {
	            return null;
			}
	    }
	}
